package logic

import (
	"beego/models"
	"github.com/astaxie/beego/orm"
	"time"
)

/** 用户新闻逻辑 **/

type NewsLogic struct{}

type info struct {
	Id          int
	Catid       int
	Title       string
	Description string
	Author      string
	Inputtime   int64
	InputDate   string
	Hits        int
}

/**
 * 结构体返回
 */

func (c *NewsLogic) DetailGet(Id int) (info, error, int64) {
	// 连接制定表对象,给指定检索查询字段赋值
	news := models.News{}
	news.Id = Id
	// 实例化ORM数据连接对象（实例化是面向对象语言的特性，这里借用表达意思）;查询赋值到对象变量
	OutMysql := orm.NewOrm()
	err := OutMysql.Read(&news)

	info := info{}
	info.Id = news.Id
	info.Catid = news.Catid
	info.Title = news.Title
	info.Description = news.Description
	info.Author = news.Author
	info.Hits = news.Hits
	info.Inputtime = news.Inputtime
	info.InputDate = time.Unix(info.Inputtime, 0).Format("2006-01-02 15:04:05")

	// 同栏目下文章总数
	cnt, _ := OutMysql.QueryTable("hz_1_news").Filter("catid", news.Catid).Count()
	return info, err, cnt
}

type newsList struct {
	Id          int
	Catid       int
	Title       string
	Description string
	Author      string
	Inputtime   int64
	Content     string
	Status      int
	InputDate   string
}

/**
 * 切片结构体
 */

func (c *NewsLogic) CateList(Catid int) []newsList {
	// 二维切片(声明临时变量)
	var newsList []newsList
	// 获取 QueryBuilder 对象. 需要指定数据库驱动参数
	// 第二个返回值是错误对象，在这里略过;构建查询对象
	var field string = "a.id,a.catid,a.title,a.description,a.author,a.inputtime,b.content"
	qb, _ := orm.NewQueryBuilder("mysql")
	qb.Select(field).From("hz_1_news a").
		InnerJoin("hz_1_news_data_0 b").On("a.id = b.id").
		Where("a.catid = ?").
		OrderBy("a.id").Desc().Limit(10).Offset(0)

	// 导出SQL语句,执行SQL语句
	sql := qb.String()
	OutMysql := orm.NewOrm()
	OutMysql.Raw(sql, Catid).QueryRows(&newsList)

	for index, value := range newsList {
		newsList[index].Status = index % 2
		newsList[index].InputDate =
			time.Unix(value.Inputtime, 0).Format("2006-01-02 15:04:05")
	}
	return newsList
}

/**
 * 根据添加查询数据表数据，RowsToMap类似ThinkPHP中column方法。
 */
func (c *NewsLogic) DataToMap(Id int) (map[string]interface{}, int64, []orm.Params, []orm.ParamsList, orm.ParamsList) {
	// 构建查询对象
	qb, _ := orm.NewQueryBuilder("mysql")
	qb.Select("a.id", "a.description", "b.content").From("hz_1_news as a").
		InnerJoin("hz_1_news_data_0 as b").On("a.id = b.id").
		Where("a.id > ?").OrderBy("a.inputtime").Desc().Limit(10).Offset(0)
	sql := qb.String()

	// 执行 SQL 语句
	OutMysql := orm.NewOrm()
	// 查询key=>value
	DataMap := make(orm.Params)
	nums, _ := OutMysql.Raw("SELECT id,title,description FROM hz_1_news WHERE catid = ?", Id).
		RowsToMap(&DataMap, "id", "title")
	// 查询列表(key=>value)
	var maps []orm.Params
	_, _ = OutMysql.Raw(sql, Id).Values(&maps)
	// 查询value的slice
	var mapsList []orm.ParamsList
	_, _ = OutMysql.Raw("SELECT id,title,description FROM hz_1_news WHERE catid = ?", Id).ValuesList(&mapsList)
	// 返回单一字段的平铺 slice 数据
	var paramsList orm.ParamsList
	_, _ = OutMysql.Raw("SELECT id,title,description FROM hz_1_news WHERE catid = ?", Id).ValuesFlat(&paramsList)

	return DataMap, nums, maps, mapsList, paramsList
}

/**
 * 获取10条文献记录
 */

func (c *NewsLogic) LiteratureGet() []LiterData {
	// 二维切片(声明临时变量)
	var literData []LiterData
	// 获取 QueryBuilder 对象. 需要指定数据库驱动参数
	// 第二个返回值是错误对象，在这里略过;构建查询对象
	var field string = "a.doi,a.pmid,a.title"
	qb, _ := orm.NewQueryBuilder("mysql")
	qb.Select(field).From("hz_sci_literature a").
		Where("a.id > ?").
		OrderBy("a.id").Desc().Limit(10).Offset(0)

	// 导出SQL语句,执行SQL语句
	sql := qb.String()
	//fmt.Println(sql)
	OutMysql := orm.NewOrm()
	OutMysql.Raw(sql, 10).QueryRows(&literData)

	return literData
}

type LiterData struct {
	Doi   string
	Pmid  int
	Title string
}
